package com.yzw.phonestoreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.phonecommen.common.R;
import com.yzw.phonestoreservice.pojo.PhoneCategory;
import com.yzw.phonestoreservice.pojo.PhoneInfo;
import com.yzw.phonestoreservice.pojo.PhoneSpecs;
import com.yzw.phonestoreservice.service.PhoneCategoryService;
import com.yzw.phonestoreservice.service.PhoneInfoService;
import com.yzw.phonestoreservice.mapper.PhoneInfoMapper;
import com.yzw.phonestoreservice.service.PhoneSpecsService;
import com.yzw.phonestoreservice.utils.PhoneUtil;
import com.yzw.phonestoreservice.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YU
 * @description 针对表【phone_info(商品表)】的数据库操作Service实现
 * @createDate 2022-11-02 17:38:28
 */
@Service
public class PhoneInfoServiceImpl extends ServiceImpl<PhoneInfoMapper, PhoneInfo>
        implements PhoneInfoService {


    @Override
    public R phoneinfoList(Map<String, Object> params) {
        LambdaQueryWrapper<PhoneInfo> queryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");


        Integer pageNum = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");

        //mybatis plus 分页查询
        Page<PhoneInfo> page = new Page<>(pageNum, limit);
        queryWrapper.like(StringUtils.isNotEmpty(key), PhoneInfo::getPhoneName, key).
                or()
                .like(StringUtils.isNotEmpty(key), PhoneInfo::getPhoneDescription, key)
                .or()
                .like(StringUtils.isNotEmpty(key), PhoneInfo::getPhoneTag, key);
        Page<PhoneInfo> phoneInfoPage = this.page(page, queryWrapper);


        return R.ok().data("phoneInfoPage", phoneInfoPage);
    }

    @Override
    public R phoneinfoDel(Integer[] ids) {
        boolean b = this.removeByIds(Arrays.stream(ids).collect(Collectors.toList()));
        if (b) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @Override
    public R phoneinfoById(Integer id) {
        PhoneInfo byId = this.getById(id);
        if (byId != null) {
            return R.ok().data("phoneInfo", byId);
        }
        return R.error().message("查询失败");
    }

    @Override
    public R phoneinfoAdd(PhoneInfo phoneInfo) {
        boolean save = this.save(phoneInfo);
        return save ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    @Override
    public R phoneinfoUpdate(PhoneInfo phoneInfo) {

        boolean update = this.updateById(phoneInfo);
        return update ? R.ok() : R.error();
    }
    @Autowired
    PhoneCategoryService phoneCategoryService;

    @Override
    public Map<Object, Object> moveIndex() {
        List<PhoneInfo> phoneInfoList = this.list();
        List<PhoneinfoVo> phones = this.findByCategoryType(phoneInfoList.get(0).getCategoryType());

        List<PhoneCategory> phoneCategorieList = phoneCategoryService.list();
        List<CategorieVo> categories = phoneCategorieList.stream().map(phoneCategorie -> {
            CategorieVo categorieVo = new CategorieVo();
            //spring封装的工具类，直接给相同的属性赋值，可以代替get,set方法
            BeanUtils.copyProperties(phoneCategorie, categorieVo);
//            categorieVo.setCategoryType(phoneInfo.getCategoryType());
//            categorieVo.setPhoneDescription(phoneInfo.getPhoneDescription());
            return categorieVo;
        }).collect(Collectors.toList());

        HashMap<Object, Object> map = new HashMap<>();
        map.put("phones", phones);
        map.put("categories", categories);
        return map;

    }
    @Autowired
    private PhoneSpecsService phoneSpecsService;
    @Override
    public Map<String, Object> findSpecsByPhoneId(Integer phoneId) {

        //通过手机id查询规格参数
        LambdaQueryWrapper<PhoneSpecs> queryWrapper = new LambdaQueryWrapper<>();
        //通过手机id查询手机的规格参数
        queryWrapper.eq(PhoneSpecs::getPhoneId,phoneId);
        List<PhoneSpecs> phoneSpecsList = phoneSpecsService.list(queryWrapper);
        PhoneInfo byId = this.getById(phoneId);
        /*获取goods的数据格式*/
        HashMap<String, String> goods = new HashMap<>();
        goods.put("picture",byId.getPhoneIcon());
        /*将goods存入传给前端的data*/
        Map<String, Object> data = new HashMap<>();
        data.put("goods",goods);

        List<PhoneSpecsVo> phoneSpecsVo = phoneSpecsList.stream().map(new Function<PhoneSpecs, PhoneSpecsVo>() {
            @Override
            public PhoneSpecsVo apply(PhoneSpecs phoneSpecs) {
                PhoneSpecsVo phoneSpecsVo = new PhoneSpecsVo();
                BeanUtils.copyProperties(phoneSpecs, phoneSpecsVo);
                return phoneSpecsVo;
            }
        }).collect(Collectors.toList());
        //返回的sku数据类型
        SkuDataVo skuDataVo = new SkuDataVo();
        /*sku数据结构*/
        Map<String, Object> map = new HashMap<>();

        ArrayList<TreeVo> treeVoList = new ArrayList<>();
        /*创建tree的数据结构*/
        TreeVo treeVo = new TreeVo();
        /*tree中的v集合*/
        treeVo.setV(phoneSpecsVo);
        treeVoList.add(treeVo);
        skuDataVo.setTree(treeVoList);

        map.put("tree",treeVo);
        List<ListVo> listVo = phoneSpecsList.stream().map(new Function<PhoneSpecs, ListVo>() {
            @Override
            public ListVo apply(PhoneSpecs phoneSpecs) {
                ListVo listVo = new ListVo();
                BeanUtils.copyProperties(phoneSpecs, listVo);
                return listVo;
            }
        }).collect(Collectors.toList());
        map.put("list",listVo);

        skuDataVo.setList(listVo);
//        skuDataVo.setSku(map);
        skuDataVo.setStockNum(byId.getPhoneStock());
        skuDataVo.setPrice(byId.getPhonePrice()+"");
//        price:"2800.00",
//                stock_num:2,
//                none_sku:false,
//                hide_stock:false
        data.put("sku",skuDataVo);
        return data;
    }

    @Override
    public List<PhoneinfoVo> findByCategoryType(Integer type) {
        LambdaQueryWrapper<PhoneInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PhoneInfo::getCategoryType, type);
        List<PhoneInfo> list = this.list(queryWrapper);
        List<PhoneinfoVo> phones = list.stream().map(phoneInfo -> {
            PhoneinfoVo phoneinfoVo = new PhoneinfoVo();
            //spring封装的工具类，直接给相同的属性赋值，可以代替get,set方法
            BeanUtils.copyProperties(phoneInfo, phoneinfoVo);
//            phoneinfoVo.setPhoneId(phoneInfo.getPhoneId());
//            phoneinfoVo.setPhoneName(phoneInfo.getPhoneName());
//            phoneinfoVo.setPhonePrice(phoneInfo.getPhonePrice());
//            phoneinfoVo.setPhoneDescription(phoneInfo.getPhoneDescription());
//            phoneinfoVo.setPhoneIcon(phoneInfo.getPhoneIcon());
            phoneinfoVo.setPhoneTag(PhoneUtil.getTag(phoneInfo.getPhoneTag()));
            return phoneinfoVo;
        }).collect(Collectors.toList());

        return phones;

    }


}




